package com.djdevs.calculatorconverter;

import java.text.*;

public class ConverterParser {
	public ConverterParser() { }
	
	private String _converterType;
	private String _baseUnit;
	private String _baseUnitCode;
	private double _baseUnitValue;
	private double _baseUnitFactor;
	private double _baseUnitDefaultZero;
	
	private String _firstUnit;
	private String _firstUnitCode;
	private double _firstUnitValue;
	private double _firstUnitFactor;
	private double _firstUnitDefaultZero;
	
	private String _secondUnit;
	private String _secondUnitCode;
	private double _secondUnitValue;
	private double _secondUnitFactor;
	private double _secondUnitDefaultZero;
	
	public double getSecondUnitValue() {
		return this._secondUnitValue;
	}
	
	public double getFirstUnitValue() {
		return this._firstUnitValue;
	}
	
	public void SetBaseUnit(String baseUnit, String baseUnitCode, Double baseUnitValue, Double baseUnitFactor
			, Double baseUnitDefaultZero, String converterType)
	{
		this._baseUnit = baseUnit;
		this._baseUnitCode = baseUnitCode;
		this._baseUnitValue = baseUnitValue;
		this._baseUnitFactor = baseUnitFactor;
		this._baseUnitDefaultZero = baseUnitDefaultZero;
		this._converterType = converterType;
	}
	
	public void SetFirstUnit(String firstUnit, String firstUnitCode, Double firstUnitValue, Double firstUnitFactor, Double firstUnitDefaultZero)
	{
		this._firstUnit = firstUnit;
		this._firstUnitCode = firstUnitCode;
		this._firstUnitValue = firstUnitValue;
		this._firstUnitFactor = firstUnitFactor;
		this._firstUnitDefaultZero = firstUnitDefaultZero;
	}
	
	public void SetSecondUnit(String secondUnit, String secondUnitCode, Double secondUnitValue, Double secondUnitFactor, Double secondUnitDefaultZero)
	{
		this._secondUnit = secondUnit;
		this._secondUnitCode = secondUnitCode;
		this._secondUnitValue = secondUnitValue;
		this._secondUnitFactor = secondUnitFactor;
		this._secondUnitDefaultZero = secondUnitDefaultZero;
	}
	
	public void ConvertUnit()
	{
		if (_converterType.equalsIgnoreCase("temperature")) 
		{
			ConvertFirstToBaseUnitTemp();
			ConvertBaseToSecondUnitTemp();			
		} 
		else 
		{
			ConvertFirstToBaseUnit();
			ConvertBaseToSecondUnit();
		}
	}
	

	// Method overloading for Temperature - START
	
	private void ConvertFirstToBaseUnitTemp() 
	{
		try {
			Double interimTemp;
			if (_firstUnitFactor >= 1) 
			{
				interimTemp = _firstUnitValue - _firstUnitDefaultZero;
				_baseUnitValue = interimTemp / _firstUnitFactor;
			
			} 
			else 
			{
				_baseUnitValue = _firstUnitValue;	
			}
		}
		catch (Exception e)
		{
			// To Be handled
		}
	}
	
	private void ConvertBaseToSecondUnitTemp() 
	{
		try {
			Double interimTemp;
			if (_secondUnitFactor >= 1) 
			{
				interimTemp = _baseUnitValue * _secondUnitFactor;
				_secondUnitValue = interimTemp + _secondUnitDefaultZero;
			
			} 
			else 
			{
				_secondUnitValue = _baseUnitValue;
			}
		}
		catch (Exception e)
		{
			// To Be handled
		}
	}
	
	// Method overloading for Temperature - END

	private void ConvertFirstToBaseUnit()
	{
		try {
			_baseUnitValue = _firstUnitValue * _firstUnitFactor;
		}
		catch (Exception e)
		{
			// To Be handled
		}
	}
	
	private void ConvertBaseToSecondUnit()
	{
		try {
			_secondUnitValue = _baseUnitValue / _secondUnitFactor;
		}
		catch (Exception e)
		{
			// To Be handled
		}
	}
	
	private static final int MAX_LENGTH = 9;

	public static String FormatNumber(double number) {
		try {
	        int digitsAvailable = MAX_LENGTH - 2;
	        if (Math.abs(number) < Math.pow(10, digitsAvailable)
	                && Math.abs(number) > Math.pow(10, -digitsAvailable)) {
	            String format = "0.";
	            double temp = number;
	            for (int i = 0; i < digitsAvailable; i++) {
	                if ((temp /= 10) < 1) {
	                    format += "#";
	                }
	            }
	            return new DecimalFormat(format).format(number);
	        }
	        String format = "0.";
	        for (int i = 0; i < digitsAvailable; i++) {
	                format += "#";
	        }
	        String r = new DecimalFormat(format + "E0").format(number);
	        int lastLength = r.length() + 1;
	        while (r.length() > MAX_LENGTH && lastLength > r.length()) {
	            lastLength = r.length();
	            r = r.replaceAll("\\.?[0-9]E", "E");
	        }
	        if (r.equalsIgnoreCase("0e0")) {
				r = "0";
			}
	        return r;
		}
		catch (Exception e) {
			return "NaN";
		}
    }
}
